/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.broiler_stoke.modular.broilerstocks.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.barn.modular.barn.mapper.BizBarnMapper;
import vip.xiaonuo.broiler_stoke.modular.broilerstocks.entity.BizBroilerStocks;
import vip.xiaonuo.broiler_stoke.modular.broilerstocks.entity.BroilerStocksView;
import vip.xiaonuo.broiler_stoke.modular.broilerstocks.mapper.BizBroilerStocksMapper;
import vip.xiaonuo.broiler_stoke.modular.broilerstocks.mapper.BroilerStocksViewMapper;
import vip.xiaonuo.broiler_stoke.modular.broilerstocks.param.BizBroilerStocksAddParam;
import vip.xiaonuo.broiler_stoke.modular.broilerstocks.param.BizBroilerStocksEditParam;
import vip.xiaonuo.broiler_stoke.modular.broilerstocks.param.BizBroilerStocksIdParam;
import vip.xiaonuo.broiler_stoke.modular.broilerstocks.param.BizBroilerStocksPageParam;
import vip.xiaonuo.broiler_stoke.modular.broilerstocks.service.BizBroilerStocksService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 肉鸡库存Service接口实现类
 *
 * @author zyb
 * @date  2023/12/19 13:56
 **/
@Service
public class BizBroilerStocksServiceImpl extends ServiceImpl<BizBroilerStocksMapper, BizBroilerStocks> implements BizBroilerStocksService {

    @Resource
    private BizBarnMapper barnMapper;

    @Resource
    private BroilerStocksViewMapper broilerStocksViewMapper;

    @Override
    public Page<BroilerStocksView> page(BizBroilerStocksPageParam bizBroilerStocksPageParam) {
        QueryWrapper<BroilerStocksView> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(bizBroilerStocksPageParam.getBarnId())) {
            queryWrapper.lambda().eq(BroilerStocksView::getBarnId, bizBroilerStocksPageParam.getBarnId());
        }
        if(ObjectUtil.isNotEmpty(bizBroilerStocksPageParam.getType())) {
            queryWrapper.lambda().eq(BroilerStocksView::getType, bizBroilerStocksPageParam.getType());
        }
        if(ObjectUtil.isAllNotEmpty(bizBroilerStocksPageParam.getSortField(), bizBroilerStocksPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(bizBroilerStocksPageParam.getSortOrder());
            queryWrapper.orderBy(true, bizBroilerStocksPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(bizBroilerStocksPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(BroilerStocksView::getId);
        }
        return broilerStocksViewMapper.selectPage(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BizBroilerStocksAddParam bizBroilerStocksAddParam) {
        BizBroilerStocks bizBroilerStocks = BeanUtil.toBean(bizBroilerStocksAddParam, BizBroilerStocks.class);

        Integer quantity = bizBroilerStocksAddParam.getQuantity();
        BigDecimal totalWeight = bizBroilerStocksAddParam.getTotalWeight();
        // 计算平均重量
        BigDecimal averageWeight = totalWeight.divide(new BigDecimal(quantity), 2, BigDecimal.ROUND_HALF_UP);

        bizBroilerStocks.setAverageWeight(averageWeight);

        this.save(bizBroilerStocks);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BizBroilerStocksEditParam bizBroilerStocksEditParam) {
        BizBroilerStocks bizBroilerStocks = this.queryEntity(bizBroilerStocksEditParam.getId());
        BeanUtil.copyProperties(bizBroilerStocksEditParam, bizBroilerStocks);
        Integer quantity = bizBroilerStocksEditParam.getQuantity();
        BigDecimal totalWeight = bizBroilerStocksEditParam.getTotalWeight();
        // 计算平均重量
        BigDecimal averageWeight = totalWeight.divide(new BigDecimal(quantity), 2, BigDecimal.ROUND_HALF_UP);
        bizBroilerStocks.setAverageWeight(averageWeight);
        this.updateById(bizBroilerStocks);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BizBroilerStocksIdParam> bizBroilerStocksIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(bizBroilerStocksIdParamList, BizBroilerStocksIdParam::getId));
    }

    @Override
    public BizBroilerStocks detail(BizBroilerStocksIdParam bizBroilerStocksIdParam) {
        return this.queryEntity(bizBroilerStocksIdParam.getId());
    }

    @Override
    public BizBroilerStocks queryEntity(String id) {
        BizBroilerStocks bizBroilerStocks = this.getById(id);
        if(ObjectUtil.isEmpty(bizBroilerStocks)) {
            throw new CommonException("肉鸡库存不存在，id值为：{}", id);
        }
        return bizBroilerStocks;
    }

    @Override
    public List<BizBroilerStocks> getBroilerStocksInfo() {
        LambdaQueryWrapper<BizBroilerStocks> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(BizBroilerStocks::getType,BizBroilerStocks::getTotalWeight);
        return this.list(queryWrapper);
    }
}
